﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace Accounts
{
    public class AccountsController
    {
        public AccountsController()
        {

        }

        public List<Accounts.BLL.AccountStats> GetUserStats()
        {
            List<Accounts.BLL.AccountStats> retValue = null;

            AccountsEntities ctx = new AccountsEntities();
            List<Accounts.GroupingUsersByUsername_Result> objs = ctx.GroupingUsersByUsername().ToList();

            if (objs.Any())
            {
                retValue = new List<BLL.AccountStats>();
                foreach (GroupingUsersByUsername_Result o in objs)
                {
                    retValue.Add(new BLL.AccountStats() { FirstChar=o.first_char, ElementsCount=o.number.GetValueOrDefault(0) });
                }
            }

            return retValue;
        }

        public List<Accounts.BLL.Account> GetAccounts(string startingChar)
        {
            List<Accounts.BLL.Account> retValue = null;

            try
            {
                AccountsEntities ctx = new AccountsEntities();
                var objs = from x in ctx.aspnet_Membership
                           join y in ctx.aspnet_Users on x.UserId equals y.UserId
                           where y.UserName.StartsWith(startingChar)
                           select new Accounts.BLL.Account
                           {
                               EMail = x.Email,
                               IsApproved = x.IsApproved,
                               IsLockedOut = x.IsLockedOut,
                               RegistrationDate = x.CreateDate,
                               UserId = x.UserId,
                               Username = y.UserName
                           };

                if (objs.Any())
                {
                    retValue = objs.ToList();
                }
            }
            catch (Exception ex)
            {
                // throw ex;
            }

            return retValue;
        }
    }
}
